﻿using LeEdu.DAL.DataAccess;
using LeEdu.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeEdu.Service
{
    /// <summary>
    /// 课时管理
    /// </summary>
    public class HourService
    {
        private NodeDal nodeDal;
        private HourDal hourDal;

        public HourService()
        {
            nodeDal = new NodeDal();
            hourDal = new HourDal();
        }

        //添加章或节
        public bool AddNode(string courseId, string name, NodeType nodeType)
        {
            Node node = new Node();
            node.Id = Guid.NewGuid();
            node.CreateTime = DateTime.Now;
            node.Name = name;
            node.CourseId = new Guid(courseId);

            node.Type = (NodeType)nodeType;// NodeType.Chapter;

            //获取序号
            var order = nodeDal.GetNodeLastOrder(node.CourseId);
            node.Order = order;

            var rtv = nodeDal.Insert(node);
            return rtv;
        }
        //添加章
        public bool AddSection(string courseId, string name)
        {
            Node node = new Node();
            node.Id = Guid.NewGuid();
            node.CreateTime = DateTime.Now;
            node.Name = name;
            node.CourseId = new Guid(courseId);
            node.Type = NodeType.Section;

            //获取序号
            var order = nodeDal.GetNodeLastOrder(node.CourseId);
            node.Order = order;

            var rtv = nodeDal.Insert(node);
            return rtv;
        }
        //添加课时
        public bool AddHour(Guid courseId, string name)
        {
            Node node = new Node();
            node.Id = Guid.NewGuid();
            node.CreateTime = DateTime.Now;
            node.Name = name;
            node.CourseId = courseId;
            node.Type = NodeType.Hour;

            var isNodeSuccess = nodeDal.Insert(node);

            Hour hour = new Hour();
            hour.Id = node.Id;
            hour.CreateTime = DateTime.Now;
            var isHourSuccess = hourDal.Insert(hour);

            return isNodeSuccess && isHourSuccess;
        }

        /// <summary>
        /// 获取课程课时列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<Node> GetNodeList(Guid uid)
        {
            var nodes = nodeDal.GetNodeListByOrder(uid);
            return nodes;
        }

        /// <summary>
        /// 更新课程排序
        /// </summary>
        /// <param name="nodes">1、数组的序号就是新的排序序号；2、格式：标识-Guid，解析时通过字符串拆分</param>
        /// <returns></returns>
        public bool UpdateNodeSort(string[] ids)
        {
            return nodeDal.UpdateNodeSort(ids);
        }

        /// <summary>
        /// 删除章
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool DeleteChapter(Guid uid)
        {
            //删除课时

            //删除节

            //删除章
            return false;
        }

        /// <summary>
        /// 删除节
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool DeleteNode(Guid uid)
        {
            //删除课时

            //删除节

            return false;
        }
        /// <summary>
        /// 删除章/节
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelNode(Guid id)
        {
            return nodeDal.DeleteById(id);
        }
        /// <summary>
        /// 删除课时
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelHour(Guid id)
        {

            return nodeDal.DeleteById(id) && hourDal.DeleteById(id);
        }

         

        /// <summary>
        /// 编辑章
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool EditChapter(Node node)
        {
            //删除课时

            //删除节

            //删除章
            return false;
        }

        /// <summary>
        /// 编辑节
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool EditNode(Node node)
        {
            return false;
        }

        /// <summary>
        /// 编辑课时
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool EditHour(Hour hour)
        {
            return hourDal.Update(hour);
        }


        /// <summary>
        /// 根据课时ID获取课时
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public Hour GetHourById(Guid hid)
        {
            return hourDal.SelectById(hid);
        }
    }
}
